package com.storeroompda.dialogs;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import com.storeroompda.R;
import com.storeroompda.bean.ItemBean;
import com.storeroompda.bean.NameEvent;
import com.storeroompda.ui.enterstore.ItemAdapter;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.util.ArrayList;
import java.util.List;

public class PopDialog extends PopupWindow {
    private String tag = "PopDialog";

    public interface PriorityListener {
        void setActivityText(ItemBean ib);
    }

    private PriorityListener listener;
    private Activity mContext;
    private LayoutInflater mInflater;
    private View mContentView;
    private List<ItemBean> beanList = new ArrayList<ItemBean>();
    private List<ItemBean> beanListtemp = new ArrayList<ItemBean>();
    private ListView listview;
    ItemAdapter ia;

    public PopDialog(Activity context, List<ItemBean> list,PriorityListener listen) {
        super(context);
        EventBus.getDefault().register(this);
        this.beanList.addAll(list);
        this.beanListtemp.addAll(list);
        this.mContext = context;
        this.listener = listen;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mContentView = mInflater.inflate(R.layout.dialog_data, null);
        setContentView(mContentView);
        setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
        setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        setBackgroundDrawable(mContext.getResources().getDrawable(android.R.color.transparent));
        setFocusable(true);
        setOutsideTouchable(true);
        setTouchable(true);
        setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                    return true;
                }
                return false;
            }
        });

        WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
        lp.alpha = 0.5f;
        mContext.getWindow().setAttributes(lp);
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
                lp.alpha = 1f;
                mContext.getWindow().setAttributes(lp);
            }
        });
        listview = (ListView) mContentView.findViewById(R.id.listview);
        ia = new ItemAdapter(mContext, beanListtemp);
        listview.setAdapter(ia);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                listener.setActivityText(beanListtemp.get(position));
                dismiss();
            }
        });
    }

    @Override
    public void dismiss() {
        EventBus.getDefault().unregister(this);
        super.dismiss();
    }

    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void onNameEvent(NameEvent nameEvent) {
        beanListtemp.clear();
        if(nameEvent.getMessage().length()==0)
        {
            beanListtemp.addAll(beanList);
        }
        else
        {
            for (int i = 0; i <beanList.size() ; i++) {
                if(beanList.get(i).getId().contains(nameEvent.getMessage()) || beanList.get(i).getName().contains(nameEvent.getMessage()))
                {
                    ItemBean itemBean =new ItemBean();
                    itemBean.setId(beanList.get(i).getId());
                    itemBean.setName(beanList.get(i).getName());
                    beanListtemp.add(itemBean);
                }
            }
        }
        ia = new ItemAdapter(mContext, beanListtemp);
        listview.setAdapter(ia);
    }
}


